//
//  ZPFilterViewController.m
//  ZPIOSEducation
//
//  Created by Encoder on 17/1/9.
//  Copyright © 2017年 mini1. All rights reserved.
//

#import "ZPFilterViewController.h"
#import "ZPFirstTableViewCell.h"
#import "ZPSecondTableViewCell.h"

@interface ZPFilterViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *firstTable;
@property (weak, nonatomic) IBOutlet UITableView *secondTable;

@property (strong,nonatomic) NSArray *filter1Array;
@property (strong,nonatomic) NSArray *filter2Array;
@property (strong,nonatomic) NSDictionary *filter2dic;

@property (strong,nonatomic) NSString *firstSelected;
@property (strong,nonatomic) NSString *secondSelected;

@property (strong,nonatomic) NSMutableDictionary *paramDic;

@end

@implementation ZPFilterViewController

-(instancetype)initWithParam:(NSDictionary *)dic{
    self = [super init];
    if (self) {
        NSMutableDictionary *tmp=[NSMutableDictionary dictionary];
        [dic enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
            if ([key isEqualToString:@"classYearTree"]) {
                NSMutableArray *arr=[obj mutableCopy];
                [arr insertObject:@{@"classYear":@"全部"} atIndex:0];
                [tmp setObject:arr forKey:key];
            }
            if ([key isEqualToString:@"courceTree"]) {
                NSMutableArray *arr=[obj mutableCopy];
                [arr insertObject:@{@"courceId":@"",@"courceName":@"全部"} atIndex:0];
                [tmp setObject:arr forKey:key];
            }
            if ([key isEqualToString:@"courseClassifyTree"]) {
                NSMutableArray *arr=[obj mutableCopy];
                [arr insertObject:@{@"courseClassifyId":@"",@"courseClassifyName":@"全部"} atIndex:0];
                [tmp setObject:arr forKey:key];
            }
            if ([key isEqualToString:@"gradeTree"]) {
                NSMutableArray *arr=[obj mutableCopy];
                [arr insertObject:@{@"classGradeId":@"",@"gradeName":@"全部"} atIndex:0];
                [tmp setObject:arr forKey:key];
            }
            if ([key isEqualToString:@"schoolTree"]) {
                NSMutableArray *arr=[obj mutableCopy];
                [arr insertObject:@{@"schoolId":@"",@"schoolName":@"全部"} atIndex:0];
                [tmp setObject:arr forKey:key];
            }
            if ([key isEqualToString:@"seasonTree"]) {
                NSMutableArray *arr=[obj mutableCopy];
                [arr insertObject:@{@"seasonId":@"",@"seasonName":@"全部"} atIndex:0];
                [tmp setObject:arr forKey:key];
            }
            
            _filter2dic = [NSDictionary dictionaryWithDictionary:tmp];
            _paramDic = [NSMutableDictionary dictionary];
            [_paramDic addEntriesFromDictionary:@{@"classYear":@"全部",@"courseId":@"",@"classGradeId":@"",@"courseClassifyId":@"",@"schoolId":@"",@"classSeasonId":@""}];
        }];
        
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _filter1Array = [NSArray arrayWithObjects:@"年份",@"季节",@"科目",@"年级",@"课程分类", nil];
    _firstSelected = @"classYearTree";
    _filter2Array = [NSArray array];
    
    if (_paramDic==nil) {
        _paramDic = [NSMutableDictionary dictionary];
    }
    
    [_firstTable registerNib:[UINib nibWithNibName:@"ZPFirstTableViewCell" bundle:nil] forCellReuseIdentifier:@"ZPFirstTableViewCell"];
    [_firstTable setTableFooterView:[UIView new]];
    [_secondTable registerNib:[UINib nibWithNibName:@"ZPSecondTableViewCell" bundle:nil] forCellReuseIdentifier:@"ZPSecondTableViewCell"];
    
    [_firstTable selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionNone];
    [self tableView:_firstTable didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

}
- (IBAction)commit:(UIButton *)sender {
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    switch (tableView.tag) {
        case 1001:{
            return _filter1Array.count;
        }
            break;
        case 1002:{
            return _filter2Array.count;
        }
            break;
    }
    return 0;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (tableView.tag) {
        case 1001:{
            ZPFirstTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZPFirstTableViewCell"];
            cell.itemLab.text = [_filter1Array objectAtIndex:indexPath.row];
            return cell;
        }
            break;
        case 1002:{
            ZPSecondTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZPSecondTableViewCell"];
            if ([_firstSelected isEqualToString:@"classYearTree"]) {
                cell.itemLab.text = [[_filter2Array objectAtIndex:indexPath.row] objectForKey:@"classYear"];
            }else if ([_firstSelected isEqualToString:@"gradeTree"]){
                cell.itemLab.text = [[_filter2Array objectAtIndex:indexPath.row] objectForKey:@"gradeName"];
                
            }else if ([_firstSelected isEqualToString:@"courceTree"]){
                cell.itemLab.text = [[_filter2Array objectAtIndex:indexPath.row] objectForKey:@"courceName"];
                
            }else if ([_firstSelected isEqualToString:@"seasonTree"]){
                cell.itemLab.text = [[_filter2Array objectAtIndex:indexPath.row] objectForKey:@"seasonName"];
                
            }else if ([_firstSelected isEqualToString:@"schoolTree"]){
                cell.itemLab.text = [[_filter2Array objectAtIndex:indexPath.row] objectForKey:@"schoolName"];
                
            }else if ([_firstSelected isEqualToString:@"courseClassifyTree"]){
                cell.itemLab.text = [[_filter2Array objectAtIndex:indexPath.row] objectForKey:@"courseClassifyName"];
                
            }else{
                cell.itemLab.text = @"";
            }
            return cell;
        }
            break;
    }
    return nil;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (tableView.tag) {
        case 1001:{
            NSString *tmp = [_filter1Array objectAtIndex:indexPath.row];
            if ([tmp isEqualToString:@"年份"]) {
                _firstSelected = @"classYearTree";
            }else if ([tmp isEqualToString:@"年级"]){
                _firstSelected = @"gradeTree";

            }else if ([tmp isEqualToString:@"科目"]){
                _firstSelected = @"courceTree";

            }else if ([tmp isEqualToString:@"季节"]){
                _firstSelected = @"seasonTree";

            }else if ([tmp isEqualToString:@"学校"]){
                _firstSelected = @"schoolTree";

            }else if ([tmp isEqualToString:@"课程分类"]){
                _firstSelected = @"courseClassifyTree";
                
            }else{
                _firstSelected = @"classYearTree";
            }
            _filter2Array = [_filter2dic objectForKey:_firstSelected];
            [_secondTable reloadData];
            [_secondTable selectRowAtIndexPath:[self makeSelect] animated:YES scrollPosition:UITableViewScrollPositionNone];
        }
            break;
        case 1002:{
            if ([_firstSelected isEqualToString:@"classYearTree"]) {
                [_paramDic setObject:[[_filter2Array objectAtIndex:indexPath.row] objectForKey:@"classYear"] forKey:@"classYear"];
            }else if ([_firstSelected isEqualToString:@"gradeTree"]){
                [_paramDic setObject:[[_filter2Array objectAtIndex:indexPath.row] objectForKey:@"classGradeId"] forKey:@"classGradeId"];
            }else if ([_firstSelected isEqualToString:@"courceTree"]){
                [_paramDic setObject:[[_filter2Array objectAtIndex:indexPath.row] objectForKey:@"courceId"] forKey:@"courseId"];
            }else if ([_firstSelected isEqualToString:@"seasonTree"]){
                [_paramDic setObject:[[_filter2Array objectAtIndex:indexPath.row] objectForKey:@"seasonId"] forKey:@"classSeasonId"];
            }else if ([_firstSelected isEqualToString:@"schoolTree"]){
                [_paramDic setObject:[[_filter2Array objectAtIndex:indexPath.row] objectForKey:@"schoolId"] forKey:@"schoolId"];
            }else if ([_firstSelected isEqualToString:@"courseClassifyTree"]){
                [_paramDic setObject:[[_filter2Array objectAtIndex:indexPath.row] objectForKey:@"courseClassifyId"] forKey:@"courseClassifyId"];
            }else{
            }

        }
            break;
    }
}

-(NSIndexPath *)makeSelect{
    if ([_firstSelected isEqualToString:@"classYearTree"]) {
        if ([_paramDic.allKeys containsObject:@"classYear"]) {
            __block NSIndexPath *tmp = nil;
            [_filter2Array enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if ([toStr([obj objectForKey:@"classYear"]) isEqualToString:toStr([_paramDic objectForKey:@"classYear"])]) {
                    tmp = [NSIndexPath indexPathForRow:idx inSection:0];
                    *stop = YES;
                }
            }];
            return tmp;
        }else{
            return nil;
        }
    }else if ([_firstSelected isEqualToString:@"gradeTree"]){
        if ([_paramDic.allKeys containsObject:@"classGradeId"]) {
            __block NSIndexPath *tmp = nil;
            [_filter2Array enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if ([toStr([obj objectForKey:@"classGradeId"]) isEqualToString:toStr([_paramDic objectForKey:@"classGradeId"])]) {
                    tmp = [NSIndexPath indexPathForRow:idx inSection:0];
                    *stop = YES;
                }
            }];
            return tmp;
        }else{
            return nil;
        }
    }else if ([_firstSelected isEqualToString:@"courceTree"]){
        if ([_paramDic.allKeys containsObject:@"courseId"]) {
            __block NSIndexPath *tmp = nil;
            [_filter2Array enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if ([toStr([obj objectForKey:@"courseId"]) isEqualToString:toStr([_paramDic objectForKey:@"courseId"])]) {
                    tmp = [NSIndexPath indexPathForRow:idx inSection:0];
                    *stop = YES;
                }
            }];
            return tmp;
        }else{
            return nil;
        }
    }else if ([_firstSelected isEqualToString:@"seasonTree"]){
        if ([_paramDic.allKeys containsObject:@"classSeasonId"]) {
            __block NSIndexPath *tmp = nil;
            [_filter2Array enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if ([toStr([obj objectForKey:@"seasonId"]) isEqualToString:toStr([_paramDic objectForKey:@"classSeasonId"])]) {
                    tmp = [NSIndexPath indexPathForRow:idx inSection:0];
                    *stop = YES;
                }
            }];
            return tmp;
        }else{
            return nil;
        }
    }else if ([_firstSelected isEqualToString:@"schoolTree"]){
        if ([_paramDic.allKeys containsObject:@"schoolId"]) {
            __block NSIndexPath *tmp = nil;
            [_filter2Array enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if ([toStr([obj objectForKey:@"schoolId"]) isEqualToString:toStr([_paramDic objectForKey:@"schoolId"])]) {
                    tmp = [NSIndexPath indexPathForRow:idx inSection:0];
                    *stop = YES;
                }
            }];
            return tmp;
        }else{
            return nil;
        }
    }else if ([_firstSelected isEqualToString:@"courseClassifyTree"]){
        if ([_paramDic.allKeys containsObject:@"courseClassifyId"]) {
            __block NSIndexPath *tmp = nil;
            [_filter2Array enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if ([toStr([obj objectForKey:@"courseClassifyId"]) isEqualToString:toStr([_paramDic objectForKey:@"courseClassifyId"])]) {
                    tmp = [NSIndexPath indexPathForRow:idx inSection:0];
                    *stop = YES;
                }
            }];
            return tmp;
        }else{
            return nil;
        }
    }else{
        return nil;
    }
}

- (IBAction)confirmBtnAction:(UIButton *)sender {
    if (_filterBlock) {
        _filterBlock(_paramDic);
    }
}

- (IBAction)resetBtnAction:(UIButton *)sender {
    [_paramDic removeAllObjects];
    if (_resetBlock) {
        _resetBlock(@[@"courseClassifyId",@"schoolId",@"classSeasonId",@"courseId",@"classGradeId",@"classYear"]);
    }
    [_paramDic addEntriesFromDictionary:@{@"classYear":@"全部",@"courseId":@"",@"classGradeId":@"",@"courseClassifyId":@"",@"schoolId":@"",@"classSeasonId":@""}];
    [_secondTable reloadData];
    [_secondTable selectRowAtIndexPath:[self makeSelect] animated:YES scrollPosition:UITableViewScrollPositionNone];
}

-(void)clearFilter{
    [_paramDic removeAllObjects];
    if (_resetBlock) {
        _resetBlock(@[@"courseClassifyId",@"schoolId",@"classSeasonId",@"courseId",@"classGradeId",@"classYear"]);
    }
}



@end
